<template>
	<view class="car-num-page">
		<view class="num-container">
			<car-number-input @numberInputResult="numberInputResult" :defaultStr="defaultNum"></car-number-input>
		</view>
		<view class="addBtn">
			<up-button @click="addBtnNum" class="add-car-btn" color="#2878ff" :disabled="disAddBtn"> 确定添加 </up-button>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			defaultNum: ''
		};
	},
	computed:{
		disAddBtn(){
			return (this.defaultNum.length==7 || this.defaultNum.length==8) ? false:true
		}
	},
	mounted() {
	this.defaultNum =	uni.getStorageSync('userCarNum')
	},
	methods: {
		numberInputResult(e) {
			console.log('结果--' + e);
			this.defaultNum = e.trim()
			
		},
		addBtnNum(){
			uni.setStorageSync('userCarNum',this.defaultNum)
		},
		
		isValidLicensePlate(plate) {
			// 定义省份简称列表
			console.log(plate);
			const provinces = [
				'京',
				'津',
				'沪',
				'渝',
				'冀',
				'豫',
				'云',
				'辽',
				'黑',
				'湘',
				'皖',
				'鲁',
				'新',
				'苏',
				'浙',
				'赣',
				'鄂',
				'桂',
				'甘',
				'晋',
				'蒙',
				'陕',
				'吉',
				'闽',
				'贵',
				'粤',
				'青',
				'藏',
				'川',
				'宁',
				'琼'
			];

			// 正则表达式匹配小型汽车和新能源汽车车牌号
			const regex = /^([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼])\w{6}$/;
			const newEnergyRegex = /^([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼])[A-Z]D[DF]\d{4}$/i;

			if (regex.test(plate)) {
				console.log('regex');
				// 检查省份简称是否合法
				if (provinces.includes(plate[0])) {
					console.log('regex111111111');
					return true;
				}
			} else if (newEnergyRegex.test(plate)) {
				// 新能源车牌检查省份简称
				console.log('newEnergyRegex');
				if (provinces.includes(plate[0])) {
					console.log('newEnergyRegex2222222');
					return true;
				}
			}

			return false;
		}
	},
	destroyed() {
		this.defaultNum=''
	}
};
</script>

<style scoped lang="scss">
.car-num-page {
	width: 750rpx;
	margin: 0 auto;
	.add-car-btn{
		width: 682rpx;
		border-radius: 50rpx;
		margin-top: 120rpx;
	}
}
</style>
